<#import "function.ftl" as func>
<#assign package=model.variables.package>
<#assign comment=model.tabComment>
<#assign class=model.variables.class>
<#assign classVar=model.variables.classVar>
<#assign system=vars.system>
<#assign domain=vars.domain>
<#assign pkType=func.getPkType(model)>
<#assign subtables=model.subTableList>
<#assign sub=model.sub>
<#assign subSize=func.hasSubTable(model)>
<#assign author=vars.developer>

package ${domain}.${system}.${package}.service;

import ${domain}.core.common.service.impl.SuperServiceImpl;
import ${domain}.core.db.base.BaseDao;
import ${domain}.core.db.base.BaseService;
import ${domain}.${system}.${package}.entity.${class};
import ${domain}.${system}.${package}.mapper.${class}Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
<#if (subSize?number  gt 0) >
    <#assign subtables=model.subTableList>
    <#list subtables as subTable>
import ${domain}.${system}.${package}.entity.${subTable.variables.class};
import ${domain}.${system}.${package}.service.${subTable.variables.class}ServiceImpl;
    </#list>
import ${domain}.core.tool.IdGenerator;
import ${domain}.core.tool.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.ArrayList;
import java.util.List;
</#if>

/**
* ${comment}业务服务类.
* @author ${author}
*/
@Service
public class ${class}ServiceImpl extends SuperServiceImpl<${class}Mapper, ${class}> implements BaseService<${class}> {

    @Autowired
    private ${class}Mapper ${classVar}Mapper;
    <#if (subSize?number  gt 0) >
        <#assign subtables=model.subTableList>
        <#list subtables as subTable>
    @Autowired
    private ${subTable.variables.class}ServiceImpl ${subTable.variables.classVar}ServiceImpl;
        </#list>
    </#if>

    @Override
    public BaseDao<${class}> getRepository() {
        return ${classVar}Mapper;
    }

    <#if (subSize?number  gt 0)>

    /**
    * 根据子表外键删除子表数据.
    * @param pid pid
    */
    public void removeByFk(String pid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("${model.getForeignKey()}", pid);
        ${classVar}Mapper.delete(wrapper);
    }

    /**
    * 插入子表数据，设定ID和外键.
    * @param entity entity
    */
    public void addList(${model.getMainModel().variables.class} entity) {
        for (${class} ${classVar} : entity.get${class}()) {
            ${classVar}.setId(IdGenerator.getIdStr());
            ${classVar}.set${func.convertUnderLine(model.getForeignKey())?cap_first}(entity.getId());
            ${classVar}Mapper.insert(${classVar});
        }
    }

    /**
    * 根据子表外键获取数据.
    * @param fk fk
    * @return
    */
    public List<${class}> getByFk(String fk){
        QueryWrapper wrapper=new QueryWrapper<${class}>();
        wrapper.eq("${model.getForeignKey()}",fk);
        return ${classVar}Mapper.selectList(wrapper);
    }

    <#assign subtables=model.subTableList>

    /**
    * 当主表数据为新增的情况下，子表也要根据外键同时添加数据.
    * @param entity entity
    * @return
    */
    @Override
    public int insert(${class} entity) {
        if (BeanUtil.isEmpty(entity.getPkId())) {
            entity.setPkId(IdGenerator.getIdStr());
        }
        <#list subtables as subTable>
        ${subTable.variables.classVar}ServiceImpl.addList(entity);
        </#list>
        return getRepository().insert(entity);
    }

    /**
    * 更新数据的情况下，先根据外键删除子表的数据，然后直接插入新增的数据.
    * @param entity entity
    * @return
    */
    @Override
    public int update(${class} entity) {
        int rtn = ${classVar}Mapper.updateById(entity);
        <#list subtables as subTable>
        ${subTable.variables.classVar}ServiceImpl.removeByFk(entity.getPkId());
        ${subTable.variables.classVar}ServiceImpl.addList(entity);
        </#list>
        return rtn;
    }

    </#if>
}
